package entity

import (
	"fmt"
	"time"

	"gorm.io/gorm"
	"shqsoft.cn/pas3ui/base"
)

//对公存贷款批量认定
type CompAccoPl struct {
	ID     int       `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	ACID   *int      `gorm:"column:ACID"` //帐户序号
	ENCD   string    `gorm:"column:ENCD"`
	ACNO   string    `gorm:"column:ACNO"`
	BANO   string    `gorm:"column:BANO"`
	KHMC   string    `gorm:"column:KHMC"`
	EMID1  *int      `gorm:"column:EMID1"`
	EMNO1  string    `gorm:"column:EMNO1"`
	ZB1    float64   `gorm:"column:ZB1"`
	EMID2  *int      `gorm:"column:EMID2"`
	EMNO2  string    `gorm:"column:EMNO2"`
	ZB2    float64   `gorm:"column:ZB2"`
	EMID3  *int      `gorm:"column:EMID3"`
	EMNO3  string    `gorm:"column:EMNO3"`
	ZB3    float64   `gorm:"column:ZB3"`
	EMID4  *int      `gorm:"column:EMID4"`
	EMNO4  string    `gorm:"column:EMNO4"`
	ZB4    float64   `gorm:"column:ZB4"`
	EMID5  *int      `gorm:"column:EMID5"`
	EMNO5  string    `gorm:"column:EMNO5"`
	ZB5    float64   `gorm:"column:ZB5"`
	EMID6  *int      `gorm:"column:EMID6"`
	EMNO6  string    `gorm:"column:EMNO6"`
	ZB6    float64   `gorm:"column:ZB6"`
	EMID7  *int      `gorm:"column:EMID7"`
	EMNO7  string    `gorm:"column:EMNO7"`
	ZB7    float64   `gorm:"column:ZB7"`
	EMID8  *int      `gorm:"column:EMID8"`
	EMNO8  string    `gorm:"column:EMNO8"`
	ZB8    float64   `gorm:"column:ZB8"`
	EMID9  *int      `gorm:"column:EMID9"`
	EMNO9  string    `gorm:"column:EMNO9"`
	ZB9    float64   `gorm:"column:ZB9"`
	EMID10 *int      `gorm:"column:EMID10"`
	EMNO10 string    `gorm:"column:EMNO10"`
	ZB10   float64   `gorm:"column:ZB10"`
	DSDT   time.Time `gorm:"column:DSDT"` //分户日期
	DSEM   *int      `gorm:"column:DSEM"`
}

//TableName 对公存贷款批量认定
func (CompAccoPl) TableName() string {
	return "T_COMP_ACCO_PL"
}

// AfterFind 查询后触发器
func (i *CompAccoPl) AfterFind(tx *gorm.DB) (err error) {
	i.ACNO = base.CdnoTM(i.ACNO)
	i.BANO = base.AcnoTM(i.BANO)
	return
}

func (i *CompAccoPl) BeforeSave(tx *gorm.DB) (err error) {
	bdate, err := base.GetBackDATE()
	if err != nil {
		return
	}
	if i.DSDT.Unix() < bdate.Unix() {
		err = fmt.Errorf("分户日期不能小于最早可追溯日期，最大追溯日期为【" + bdate.Format("2006-01-02") + "】")
		return
	}
	var count int
	base.CHDB.Raw(`SELECT "ID" FROM "T_COMP_ACCO" WHERE "ACNO" = ? AND "BANO" = ?`, i.ACNO, i.BANO).Row().Scan(&i.ACID)
	if i.ACID == nil {
		err = fmt.Errorf("账号：%s,子账号：%s,在系统中不存在或不匹配！", i.ACNO, i.BANO)
		return
	}
	//判断是否重复导入
	if i.ID == 0 {
		tx.Raw(`SELECT COUNT(*) FROM "T_COMP_ACCO_PL" WHERE "ACID"=?`, i.ACID).Row().Scan(&count)
		if count > 0 {
			err = fmt.Errorf("账号:%s,不能重复导入！", i.ACNO)
			return
		}
	}
	sum := i.ZB1 + i.ZB2 + i.ZB3 + i.ZB4 + i.ZB5 + i.ZB6 + i.ZB7 + i.ZB8 + i.ZB9 + i.ZB10
	if sum != 100 {
		err = fmt.Errorf("占比之和必须为100！")
		return
	}
	err = tx.Raw(`SELECT "ID" FROM "T_SysUser" WHERE "SysUserID" =?`, i.EMNO1).Row().Scan(&i.EMID1)
	if err != nil {
		err = fmt.Errorf("员工工号1:%s在系统中没有找到！", i.EMNO1)
		return
	}
	if i.ZB2 > 0 {
		tx.Raw(`SELECT "ID" FROM "T_SysUser" WHERE "SysUserID" =?`, i.EMNO2).Row().Scan(&i.EMID2)
		if err != nil {
			err = fmt.Errorf("员工工号2:%s在系统中没有找到！", i.EMNO2)
			return
		}
	}
	if i.ZB3 > 0 {
		tx.Raw(`SELECT "ID" FROM "T_SysUser" WHERE "SysUserID" =?`, i.EMNO3).Row().Scan(&i.EMID3)
		if err != nil {
			err = fmt.Errorf("员工工号3:%s在系统中没有找到！", i.EMNO3)
			return
		}
	}
	if i.ZB4 > 0 {
		tx.Raw(`SELECT "ID" FROM "T_SysUser" WHERE "SysUserID" =?`, i.EMNO4).Row().Scan(&i.EMID4)
		if err != nil {
			err = fmt.Errorf("员工工号4:%s在系统中没有找到！", i.EMNO4)
			return
		}
	}
	if i.ZB5 > 0 {
		tx.Raw(`SELECT "ID" FROM "T_SysUser" WHERE "SysUserID" =?`, i.EMNO5).Row().Scan(&i.EMID5)
		if err != nil {
			err = fmt.Errorf("员工工号5:%s在系统中没有找到！", i.EMNO5)
			return
		}
	}
	if i.ZB6 > 0 {
		tx.Raw(`SELECT "ID" FROM "T_SysUser" WHERE "SysUserID" =?`, i.EMNO6).Row().Scan(&i.EMID6)
		if err != nil {
			err = fmt.Errorf("员工工号6:%s在系统中没有找到！", i.EMNO6)
			return
		}
	}
	if i.ZB7 > 0 {
		tx.Raw(`SELECT "ID" FROM "T_SysUser" WHERE "SysUserID" =?`, i.EMNO7).Row().Scan(&i.EMID7)
		if err != nil {
			err = fmt.Errorf("员工工号7:%s在系统中没有找到！", i.EMNO7)
			return
		}
	}
	if i.ZB8 > 0 {
		tx.Raw(`SELECT "ID" FROM "T_SysUser" WHERE "SysUserID" =?`, i.EMNO8).Row().Scan(&i.EMID8)
		if err != nil {
			err = fmt.Errorf("员工工号8:%s在系统中没有找到！", i.EMNO8)
			return
		}
	}
	if i.ZB9 > 0 {
		tx.Raw(`SELECT "ID" FROM "T_SysUser" WHERE "SysUserID" =?`, i.EMNO9).Row().Scan(&i.EMID9)
		if err != nil {
			err = fmt.Errorf("员工工号9:%s在系统中没有找到！", i.EMNO9)
			return
		}
	}
	if i.ZB10 > 0 {
		tx.Raw(`SELECT "ID" FROM "T_SysUser" WHERE "SysUserID" =?`, i.EMNO10).Row().Scan(&i.EMID10)
		if err != nil {
			err = fmt.Errorf("员工工号10:%s在系统中没有找到！", i.EMNO10)
			return
		}
	}
	return
}

//CompAccoAfterImport 导入后执行分户处理
func CompAccoAfterImport(tx *gorm.DB, params map[string]string) (err error) {
	var sqlstr string
	if base.AppConf.Database.Mysql != "" {
		sqlstr = `UPDATE "T_COMP_ACCO_DIVI" A,"T_COMP_ACCO_PL" B
		SET "WODT"=B."DSDT","ISWO"=CASE WHEN A."DSDT"<B."DSDT" THEN 0 ELSE 1 END
		WHERE A."ACID"=B."ACID" AND A."ISWO"=0 AND (A."WODT" IS NULL OR A."WODT">B."DSDT")`
	} else {
		sqlstr = `update "T_COMP_ACCO_DIVI" A
		set "WODT"=B."DSDT","ISWO"=case when A."DSDT"<B."DSDT" then 0 else 1 end
		from "T_COMP_ACCO_PL" B
		WHERE A."ACID"=B."ACID" AND A."ISWO"=0 AND (A."WODT" IS NULL OR A."WODT">B."DSDT")`
	}
	err = tx.Exec(sqlstr).Error
	if err != nil {
		tx.Rollback()
		return
	}
	sqlstr = `INSERT INTO "T_COMP_ACCO_DIVI"("ACID","EMID","DSVL","DSDT","DSEM")
	SELECT DISTINCT "ACID","EMID","DSVL","DSDT","DSEM"
	FROM (SELECT "ACID","EMID1" "EMID","ZB1"/100 "DSVL","DSDT","DSEM" FROM "T_COMP_ACCO_PL"
	where "EMID1" is not null
	UNION ALL
	SELECT "ACID","EMID2" "EMID","ZB2"/100 "DSVL","DSDT","DSEM" FROM "T_COMP_ACCO_PL"
	where "EMID2" is not null
	UNION ALL
	SELECT "ACID","EMID3" "EMID","ZB3"/100 "DSVL","DSDT","DSEM" FROM "T_COMP_ACCO_PL"
	where "EMID3" is not null
	UNION ALL
	SELECT "ACID","EMID4" "EMID","ZB4"/100 "DSVL","DSDT","DSEM" FROM "T_COMP_ACCO_PL"
	where "EMID4" is not null
	UNION ALL
	SELECT "ACID","EMID5" "EMID","ZB5"/100 "DSVL","DSDT","DSEM" FROM "T_COMP_ACCO_PL"
	where "EMID5" is not null
	UNION ALL
	SELECT "ACID","EMID6" "EMID","ZB6"/100 "DSVL","DSDT","DSEM" FROM "T_COMP_ACCO_PL"
	where "EMID6" is not null
	UNION ALL
	SELECT "ACID","EMID7" "EMID","ZB7"/100 "DSVL","DSDT","DSEM" FROM "T_COMP_ACCO_PL"
	where "EMID7" is not null
	UNION ALL
	SELECT "ACID","EMID8" "EMID","ZB8"/100 "DSVL","DSDT","DSEM" FROM "T_COMP_ACCO_PL"
	where "EMID8" is not null
	UNION ALL
	SELECT "ACID","EMID9" "EMID","ZB9"/100 "DSVL","DSDT","DSEM" FROM "T_COMP_ACCO_PL"
	where "EMID9" is not null
	UNION ALL
	SELECT "ACID","EMID10" "EMID","ZB10"/100 "DSVL","DSDT","DSEM" FROM "T_COMP_ACCO_PL"
	where "EMID10" is not null) A`
	err = tx.Exec(sqlstr).Error
	if err != nil {
		tx.Rollback()
		return
	}
	tx.Commit()
	return
}

func CompAccoPlEF() base.EntityFunc {
	return func() interface{} {
		entity := CompAccoPl{}
		return &entity
	}
}
func CompAccoPlESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []CompAccoPl
		return &entities
	}
}

func init() {
	base.DaoPoolObj.Put("CompAccoPl", base.NewDefaultDao(CompAccoPlEF(), CompAccoPlESF(), nil, "my"))
	base.SetAfterCallBack("CompAccoPl", CompAccoAfterImport)
}
